package http

import (
	"gitee.com/zhangyu520cw/restful-api-demo-g7/apps/host"
	"github.com/gin-gonic/gin"
	"github.com/infraboard/mcube/http/response"
)

// 用于暴漏Host service接口
// 所以需要依赖内部接口的实现
// 该实体类会实现gin的http Handler
func (h *Handler) createHost(c *gin.Context) {
	ins := host.NewHost()

	// read c.
	//用户传递过来的参数进行一个解析，实现了一个json 的unmarshal
	if err := c.Bind(ins); err != nil {
		response.Failed(c.Writer, err)
		return
	}

	//进行接口调用
	ins, err := h.svc.CreateHost(c.Request.Context(), ins)
	if err != nil {
		response.Failed(c.Writer, err)
		return
	}
	response.Success(c.Writer, ins)
}

func (h *Handler) queryHost(c *gin.Context) {
	// 从http请求的query stri
	req := host.NewQueryHostFromHTTP(c.Request)
	ins, err := h.svc.QueryHost(c.Request.Context(), req)
	if err != nil {
		response.Failed(c.Writer, err)
		return
	}
	response.Success(c.Writer, ins)
}
